<template>
  <!-- 子系统管理/下级菜单 -->
  <div class="sub-menu">
    <el-dialog
      title="新增下级菜单"
      :visible.sync="dialogVisible"
      width="50%"
      :before-close="handleClose"
    >
      <el-row style="margin-bottom:30px;">
        <el-radio v-model="radio" label="1">业务菜单</el-radio>
        <el-radio v-model="radio" label="2">导航菜单</el-radio>
      </el-row>
      <!-- 业务菜单 -->
      <div>
        <el-form :inline="true" :model="formData">
          <el-form-item label="检索">
            <el-input
              v-model="formData.name"
              clearable
              placeholder="请输入菜单名称"
            />
          </el-form-item>
          <el-form-item>
            <el-button
              :loading="formLoading"
              type="primary"
              @click.stop="search()"
              >查询</el-button
            >
          </el-form-item>
        </el-form>
        <!-- 业务菜单 -->
        <el-editable
          v-show="radio === '1'"
          ref="businessMenu"
          v-loading="addMenuLoading"
          :data="menuList"
          height="200"
          :columns="menuColumns"
        >
          <template slot="default-change" slot-scope="scope">
            <div>
              <el-radio v-model="tableRadio" :label="scope.$index"
                ><i></i
              ></el-radio>
            </div>
          </template>
          <template slot="default-appLogo" slot-scope="scope">
            <div class="logo-box">
              <icon-font
                class="icon-item"
                :icon="scope.row.appLogo || 'el-icon-setting'"
              />
            </div>
          </template>
        </el-editable>
        <el-editable
          v-show="radio === '2'"
          ref="businessMenu"
          v-loading="addMenuLoading"
          :data="menuList"
          height="200"
          :columns="navMenuColumns"
        >
          <template slot="default-change" slot-scope="scope">
            <div>
              <el-radio v-model="tableRadio" :label="scope.$index"
                ><i></i
              ></el-radio>
            </div>
          </template>
          <template slot="default-appLogo" slot-scope="scope">
            <div class="logo-box">
              <icon-font
                class="icon-item"
                :icon="scope.row.appLogo || 'el-icon-setting'"
              />
            </div>
          </template>
        </el-editable>
        <el-pagination
          align="right"
          style="margin-top: 10px;"
          @size-change="sizeChange"
          @current-change="pageChange"
          :page-sizes="PAGE_NUMBER"
          :page-size="page.size"
          :current-page="page.page"
          :layout="PAGINATION_LAYOUT"
          :total="page.total"
        />
      </div>
      <span slot="footer" class="dialog-footer">
        <el-button @click="dialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="submitForm">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>
<script>
import ElEditable from '@components/el-editable/door/index.vue'
import {PAGINATION_LAYOUT, PAGE_NUMBER} from '@const/common.js'
import {hasIn, cloneDeep} from 'lodash'
import {
  resourcePageList,
  resourceUpdateStatusById,
  appResourceAdd,
  menuDeleteById
} from '@const/api_staffCenter.js'
import {menuColumns, navMenuColumns} from './const.js'
export default {
  components: {ElEditable},
  data() {
    return {
      dialogVisible: false,
      radio: '1',
      tableRadio: '',
      formData: {
        name: ''
      },
      formDataNav: {
        name: null
      },
      rulesnav: {
        resourceName: [
          {required: true, message: '请输入活动名称', trigger: 'blur'}
        ]
      },
      formLoading: false,
      addMenuLoading: false,
      menuList: [],
      page: {
        size: 20,
        page: 1,
        total: 0
      },
      PAGE_NUMBER, // 页码数组
      PAGINATION_LAYOUT, // 页码功能布局
      menuColumns,
      navMenuColumns,
      columns: menuColumns,
      curSelectNode: {},
      multipleSelection: []
    }
  },
  watch: {
    radio(val) {
      this.page.page = 1
      this.search()
    }
  },
  mounted() {},
  methods: {
    showDialog(curSelectNode) {
      this.curSelectNode = curSelectNode
      this.dialogVisible = true
      this.search()
    },
    handleClose(done) {
      done()
    },
    pageChange(val) {
      this.page.page = val
      this.search()
    },
    sizeChange(val) {
      this.page.size = val
      this.search()
    },
    submitForm() {
      if (this.tableRadio === '') {
        this.$message.error('请选择菜单')
        return false
      }
      let params = {
        appId: this.curSelectNode.appId || this.$route.query.id,
        resourceId: this.menuList[this.tableRadio].id,
        parentId: this.curSelectNode.resourceType
          ? this.curSelectNode.id || '0'
          : '0',
        resourceName: this.menuList[this.tableRadio].resourceName,
        resourceType: this.radio === '1' ? 1 : 3,
        resourceIcon: this.menuList[this.tableRadio].resourceIcon,
        remark: this.radio === '1' ? '' : this.formDataNav.remark
      }
      this.$axios({
        url: appResourceAdd,
        method: 'post',
        data: params
      })
        .then(res => {
          if (res.data.msg === 'ok') {
            this.$emit('getAppsTree')
            this.$message.success('新增成功')
          }
        })
        .finally(() => {
          this.treeLoading = false
          this.dialogVisible = false
        })
    },
    search() {
      let resourceType = this.radio === '1' ? 1 : 3
      this.$axios({
        url: `${resourcePageList}?page=${this.page.page}&pageSize=${this.page.size}&status=1&resourceName=${this.formData.name}&resourceType=${resourceType}`,
        method: 'get'
      })
        .then(res => {
          let data = hasIn(res, ['data', 'payload']) ? res.data.payload : []
          this.menuList = data.content
          this.page.total = data.totalElements
        })
        .finally(() => {
          this.treeLoading = false
        })
    }
  }
}
</script>
<style lang="less"></style>
